package com.xhwl.data.aop;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.utils.IpUtil;
import com.xhwl.common.utils.ThreadLocalUtil;
import com.xhwl.data.util.RedisUtils;
import com.xhwl.data.util.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Objects;

/**
 * @Author JCccc
 * @Description
 * @Date 2021/8/18 16:15
 */
@Component
public class ThreadLocalInterceptor implements HandlerInterceptor {
    public static final Logger log = LoggerFactory.getLogger(ThreadLocalInterceptor.class);
    private String ACCOUNT_SAVE_PREFIX = "USERINFO_";
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        String ipAddr = IpUtil.getIpAddr(request);
        ThreadLocalUtil.setIp(ipAddr);
        if (null != request.getHeader("industryId")) {
            Short industryId = Short.valueOf(request.getHeader("industryId"));
            ThreadLocalUtil.setIndustryId(industryId);
        }
        String userId = request.getHeader("userId");
        String appId = request.getHeader("appId");
        RedisUtils redisUtils = SpringContextUtils.getBean(RedisUtils.class);
        Object accountInfo = redisUtils.get(appId + "_" + ACCOUNT_SAVE_PREFIX + userId);
        if (Objects.nonNull(accountInfo)) {
            Account account = conversionAccount((JSONObject) accountInfo);
            ThreadLocalUtil.setAccount(account);
        }

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex){
        ThreadLocalUtil.removeThreadLocal();
    }


    /**
     * 转换自动注入的账号数据
     *
     * @param jsonObject
     * @return
     */
    private Account conversionAccount(JSONObject jsonObject) {
        Account account = new Account();
        account.setId((Integer) jsonObject.get("userId"));
        account.setIndustryId(jsonObject.getShort("industryId"));
        account.setLoginName(jsonObject.getString("loginName"));
        account.setPassword(jsonObject.getString("password"));
        account.setNickname(jsonObject.getString("nickname"));
        account.setName(jsonObject.getString("name"));
        account.setPhone(jsonObject.getString("phone"));
        account.setSex((Short) jsonObject.get("sex"));
        account.setAge(jsonObject.getInteger("age"));
        account.setFaceId(jsonObject.getString("faceId"));
        account.setFaceUrl(jsonObject.getString("url"));
        account.setStatus((Short) jsonObject.get("status"));
        account.setIsDeleted(jsonObject.getBoolean("isDeleted"));
        account.setUpdatedAt((Date) jsonObject.get("updatedAt"));
        account.setCreatedAt((Date) jsonObject.get("createdAt"));
        return account;
    }
}
